package com.example.zhangxun0527;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.zhangxun0527.MyShopDemo.User;
import com.example.zhangxun0527.fragment.BangzhuFragment;
import com.example.zhangxun0527.fragment.SheziFragment;
import com.example.zhangxun0527.fragment.ShouyeFragment;
import com.example.zhangxun0527.fragment.WoFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private ShouyeFragment shouyeFragment;
    private BangzhuFragment bangzhuFragment;
    private SheziFragment sheziFragment;
    private WoFragment woFragment;
    private TextView tv_title;
    String title = "";
    private RadioButton rb_shouye;
    private RadioButton rb_bangzhu;
    private RadioButton rb_shezhi;
    private RadioButton rb_wo;
    private RadioGroup rg_menu;
    FragmentManager manager;
    FragmentTransaction transaction;
    User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        init_fragment();
        Intent intent = getIntent();
        user= (User) intent.getSerializableExtra("user");
    }

    private void initView() {
        shouyeFragment=new ShouyeFragment();
        bangzhuFragment =new BangzhuFragment();
        sheziFragment = new SheziFragment();
        woFragment = new WoFragment();
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        transaction.replace(R.id.frameLayout,shouyeFragment);
        tv_title  = (TextView) findViewById(R.id.title);
        rb_shouye = (RadioButton) findViewById(R.id.rb_shouye);
        rb_bangzhu = (RadioButton) findViewById(R.id.rb_bangzhu);
        rb_shezhi = (RadioButton) findViewById(R.id.rb_shezhi);
        rb_wo = (RadioButton) findViewById(R.id.rb_wo);
        rg_menu = (RadioGroup) findViewById(R.id.rg_menu);
        rg_menu.setOnCheckedChangeListener(this);
    }
    public void init_fragment() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        title = "首页";
        shouyeFragment = ShouyeFragment.getInstance(title);
        transaction.replace(R.id.frameLayout, shouyeFragment);
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        transaction=manager.beginTransaction();
        switch (checkedId){
            case R.id.rb_shouye:
                title="首页";
                shouyeFragment =ShouyeFragment.getInstance(title);
                transaction.replace(R.id.frameLayout,shouyeFragment);

                tv_title.setText(title);
                break;
            case R.id.rb_bangzhu:
                title="帮助";
                transaction.replace(R.id.frameLayout,bangzhuFragment);
                tv_title.setText(title);
                break;
            case R.id.rb_shezhi:
                transaction.replace(R.id.frameLayout,sheziFragment);
                tv_title.setText("设置");
                break;
            case R.id.rb_wo:
                transaction.replace(R.id.frameLayout,woFragment);
                tv_title.setText("我");
                break;
        }
        transaction.commit();
    }
    public User message(){
        return user;
    }
}
